<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
            <el-form-item label="订单编号" prop="search">
                <el-input v-model="queryParams.order_sn" placeholder="请输入订单编号" clearable size="small"  @keyup.enter.native="handleQuery"/>
            </el-form-item>
            <el-form-item label="卡号" prop="search">
                <el-input v-model="queryParams.card_no" placeholder="请输入卡号" clearable size="small"  @keyup.enter.native="handleQuery"/>
            </el-form-item>

            <el-form-item label="支付方式" prop="pay_type">
                <el-select v-model="queryParams.pay_type" placeholder="支付方式" clearable>
                    <el-option
                        v-for="dict in dict.type.sys_cost_type"
                        :key="dict.value"
                        :label="dict.label"
                        :value="dict.value"
                    />
                </el-select>
            </el-form-item>
            <el-form-item label="打水状态" prop="is_water">
                <el-select v-model="queryParams.is_water" placeholder="支付方式" clearable>
                    <el-option
                        v-for="dict in dict.type.sys_complete_status"
                        :key="dict.value"
                        :label="dict.label"
                        :value="dict.value"
                    />
                </el-select>
            </el-form-item>
            <el-form-item label="时间：">
                <el-date-picker v-model="queryParams.status_end_day" style="width: 240px" value-format="yyyy-MM-dd" type="daterange" range-separator="-"
                                start-placeholder="开始日期" end-placeholder="结束日期"/>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
        <el-table  v-loading="loading" :data="Lists" border fit highlight-current-row>
            <el-table-column type="selection" width="55" bo align="center" label-width="100px" />
<!--            <el-table-column label="编号" align="center" prop="order_id"  width="50"/>-->
            <el-table-column label="卡号" align="center" prop="card_no"  width="120"  />
            <el-table-column label="持卡人" align="center" prop="pay_type" >
                <template slot-scope="{row}">
                    {{ row.user_info? row.user_info.nickname : "" }}
                </template>
            </el-table-column>
            <el-table-column label="所属设备" align="center" prop="pay_type" >
                <template slot-scope="{row}">
                    {{ row.water_device_info? row.water_device_info.water_title : "" }} - {{row.remark}}
                </template>
            </el-table-column>
            <el-table-column label="订单编号" align="center" prop="order_sn"  />
            <el-table-column label="消费金额" align="center" prop="amount" width="80" />
            <el-table-column label="实收金额" align="center" prop="buy_money" width="80"  />

            <el-table-column label="支付方式" align="center" prop="pay_type"  width="90" >
                <template slot-scope="{row}">
                    <dict-tag :options="dict.type.sys_cost_type" :value="row.pay_type"/>
                </template>
            </el-table-column>
            <el-table-column label="打水状态" align="center" prop="is_water"  width="80" >
                <template slot-scope="{row}">
                    <dict-tag :options="dict.type.sys_complete_status" :value="row.is_water"/>
                </template>
            </el-table-column>
            <el-table-column label="余额" align="center" prop="value_after" width="80" />

            <el-table-column label="订单时间" align="center" prop="created_time" width="150" :show-overflow-tooltip="true" />
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="80">
                <template slot-scope="{row}">
                    <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-edit"
                        @click="handleUpdate(row)"
                    >修改</el-button>
                    <el-button v-show="row.pay_type != 'default' " size="mini" type="text" icon="el-icon-edit" @click="handleRefund(row)">退款</el-button>
                </template>

            </el-table-column>
        </el-table>
        <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.page"
            :limit.sync="queryParams.limit"
            @pagination="getList"
        />
        <!-- 添加或修改部门对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="130px">
                <el-form-item label="订单编号" prop="order_sn">
                    <el-input v-model="form.order_sn" placeholder="请输入订单编号" />
                </el-form-item>
                <el-form-item label="实际支付金额" prop="buy_money">
                    <el-input v-model="form.buy_money" disabled placeholder="请输入实际支付金额" />
                </el-form-item>
                <el-form-item label="已消费金额" prop="amount">
                    <el-input v-model="form.amount" disabled placeholder="请输入片区名称" />
                </el-form-item>
                <el-form-item label="退款金额"  >
                    <el-input v-model="form.refund_price" placeholder="请输入片区名称" />
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitRefundForm">获取退款信息</el-button>
                <el-button type="primary" @click="submitForm">确 定 退 款</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>

 import {getConsumeList,refundConsumePrice,getConsumeDetailInfo} from "@/api/grzq/consume";

export default {
    name: "master_read",
    dicts:['sys_normal_disable','sys_cost_type','sys_complete_status'],
    data() {
        return {
            // 遮罩层
            loading: true,
            // 显示搜索条件
            showSearch: true,
            // 表格数据
            Lists: [],
            total: 0,
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 是否展开，默认全部展开
            isExpandAll: true,
            // 重新渲染表格状态
            refreshTable: true,
            // 查询参数
            queryParams: {
                search: undefined,
                page: 1,
                limit: 10,
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                buy_money: [
                    { required: true, message: "请选择片区", trigger: "blur" }
                ],
                refund_price: [
                    { required: true, message: "请选择片区", trigger: "blur" }
                ],

            },

        };
    },
    created() {
        this.getList();
    },
    methods: {
        /**   */
        async getList() {
            let that = this;
            that.loading = true;
            const {data, status, msg} = await getConsumeList(this.queryParams);
            that.Lists = data.data;
            that.loading = false;
            that.total = data.total;
            that.queryParams.limit = data.per_page || 10;
        },

        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: undefined,
                title: undefined,
                remark: undefined,
                is_check: "0"
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        /** 新增按钮操作 */
        handleAdd(row) {
            this.reset();
            this.open = true;
            this.title = "添加片区";
            this.getAllCom()
        },
        handleUpdate(){

        },
        //退款
        async submitRefundForm(row){
            const {data, status, msg} = await getConsumeDetailInfo({order_id:this.form.order_id})

            this.form.refund_price = data.refund_price
        },
          handleRefund(row){
            if (row.pay_type != 'default') {
                this.title = '退款';
                this.open = true;
                this.form = row
                this.form.refund_price = (row.buy_money - row.amount - row.refund_price).toFixed(2);
            }
        },

        /** 提交按钮 */
        submitForm: function() {
            this.$refs['form'].validate(valid => {
                if (valid) {
                    if (this.form.refund_price != undefined) {
                        refundConsumePrice({
                            order_id: this.form.order_id,
                            order_sn: this.form.order_sn,
                            buy_money: this.form.buy_money,
                            amount: this.form.amount,
                            refund_price: this.form.refund_price
                        }).then(response => {
                            const {msg,data,status} = response;
                            if (status==1){
                                this.msgError(msg);
                            }else {
                                this.msgSuccess("修改成功");
                                this.open = false;
                                this.getList();
                            }
                        })
                    }
                }
            })
        },
    }
};
</script>
